﻿CREATE PROCEDURE [dbo].[yaf_board_create](
                @BoardName     NVARCHAR(50),
                @AllowThreaded BIT,
                @UserName      NVARCHAR(50),
                @UserEmail     NVARCHAR(50),
                @UserPass      NVARCHAR(32),
                @IsHostAdmin   BIT)
AS
    BEGIN
        DECLARE  @BoardID INT
        DECLARE  @TimeZone INT
        DECLARE  @ForumEmail NVARCHAR(50)
        DECLARE  @GroupIDAdmin INT
        DECLARE  @GroupIDGuest INT
        DECLARE  @GroupIDMember INT
        DECLARE  @AccessMaskIDAdmin INT
        DECLARE  @AccessMaskIDModerator INT
        DECLARE  @AccessMaskIDMember INT
        DECLARE  @AccessMaskIDReadOnly INT
        DECLARE  @UserIDAdmin INT
        DECLARE  @UserIDGuest INT
        DECLARE  @RankIDAdmin INT
        DECLARE  @RankIDGuest INT
        DECLARE  @RankIDNewbie INT
        DECLARE  @RankIDMember INT
        DECLARE  @RankIDAdvanced INT
        DECLARE  @CategoryID INT
        DECLARE  @ForumID INT
        DECLARE  @UserFlags INT
        SET @TimeZone = (SELECT CAST(CAST([VALUE] AS NVARCHAR(50)) AS INT)
                         FROM   yaf_Registry
                         WHERE  Lower([Name]) = Lower('TimeZone'))
        SET @ForumEmail = (SELECT CAST([VALUE] AS NVARCHAR(50))
                           FROM   yaf_Registry
                           WHERE  Lower([Name]) = Lower('ForumEmail'))
        -- yaf_Board
        INSERT INTO yaf_Board
                   (Name,
                    AllowThreaded)
        VALUES     (@BoardName,
                    @AllowThreaded)
        SET @BoardID = Scope_identity()
        -- yaf_Rank
        INSERT INTO yaf_Rank
                   (BoardID,
                    Name,
                    Flags,
                    MinPosts)
        VALUES     (@BoardID,
                    'Administration',
                    0,
                    NULL)
        SET @RankIDAdmin = Scope_identity()
        INSERT INTO yaf_Rank
                   (BoardID,
                    Name,
                    Flags,
                    MinPosts)
        VALUES     (@BoardID,
                    'Guest',
                    0,
                    NULL)
        SET @RankIDGuest = Scope_identity()
        INSERT INTO yaf_Rank
                   (BoardID,
                    Name,
                    Flags,
                    MinPosts)
        VALUES     (@BoardID,
                    'Newbie',
                    3,
                    0)
        SET @RankIDNewbie = Scope_identity()
        INSERT INTO yaf_Rank
                   (BoardID,
                    Name,
                    Flags,
                    MinPosts)
        VALUES     (@BoardID,
                    'Member',
                    2,
                    10)
        SET @RankIDMember = Scope_identity()
        INSERT INTO yaf_Rank
                   (BoardID,
                    Name,
                    Flags,
                    MinPosts)
        VALUES     (@BoardID,
                    'Advanced Member',
                    2,
                    30)
        SET @RankIDAdvanced = Scope_identity()
        -- yaf_AccessMask
        INSERT INTO yaf_AccessMask
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Admin Access Mask',
                    1023)
        SET @AccessMaskIDAdmin = Scope_identity()
        INSERT INTO yaf_AccessMask
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Moderator Access Mask',
                    487)
        SET @AccessMaskIDModerator = Scope_identity()
        INSERT INTO yaf_AccessMask
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Member Access Mask',
                    423)
        SET @AccessMaskIDMember = Scope_identity()
        INSERT INTO yaf_AccessMask
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Read Only Access Mask',
                    1)
        SET @AccessMaskIDReadOnly = Scope_identity()
        -- yaf_Group
        INSERT INTO yaf_Group
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Administration',
                    1)
        SET @GroupIDAdmin = Scope_identity()
        INSERT INTO yaf_Group
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Guest',
                    2)
        SET @GroupIDGuest = Scope_identity()
        INSERT INTO yaf_Group
                   (BoardID,
                    Name,
                    Flags)
        VALUES     (@BoardID,
                    'Member',
                    4)
        SET @GroupIDMember = Scope_identity()
        SET @UserFlags = 2
        -- yaf_User
        INSERT INTO yaf_User
                   (BoardID,
                    RankID,
                    Name,
                    Password,
                    Joined,
                    LastVisit,
                    NumPosts,
                    TimeZone,
                    Email,
                    Gender,
                    Flags)
        VALUES     (@BoardID,
                    @RankIDGuest,
                    'Guest',
                    'na',
                    Getdate(),
                    Getdate(),
                    0,
                    @TimeZone,
                    @ForumEmail,
                    0,
                    @UserFlags)
        SET @UserIDGuest = Scope_identity()
        IF @IsHostAdmin <> 0
        SET @UserFlags = 3
        INSERT INTO yaf_User
                   (BoardID,
                    RankID,
                    Name,
                    Password,
                    Joined,
                    LastVisit,
                    NumPosts,
                    TimeZone,
                    Email,
                    Gender,
                    Flags)
        VALUES     (@BoardID,
                    @RankIDAdmin,
                    @UserName,
                    @UserPass,
                    Getdate(),
                    Getdate(),
                    0,
                    @TimeZone,
                    @UserEmail,
                    0,
                    @UserFlags)
        SET @UserIDAdmin = Scope_identity()
        -- yaf_UserGroup
        INSERT INTO yaf_UserGroup
                   (UserID,
                    GroupID)
        VALUES     (@UserIDAdmin,
                    @GroupIDAdmin)
        INSERT INTO yaf_UserGroup
                   (UserID,
                    GroupID)
        VALUES     (@UserIDGuest,
                    @GroupIDGuest)
        -- yaf_Category
        INSERT INTO yaf_Category
                   (BoardID,
                    Name,
                    SortOrder)
        VALUES     (@BoardID,
                    'Test Category',
                    1)
        SET @CategoryID = Scope_identity()
        -- yaf_Forum
        INSERT INTO yaf_Forum
                   (CategoryID,
                    Name,
                    Description,
                    SortOrder,
                    NumTopics,
                    NumPosts,
                    Flags)
        VALUES     (@CategoryID,
                    'Test Forum',
                    'A test forum',
                    1,
                    0,
                    0,
                    4)
        SET @ForumID = Scope_identity()
        -- yaf_ForumAccess
        INSERT INTO yaf_ForumAccess
                   (GroupID,
                    ForumID,
                    AccessMaskID)
        VALUES     (@GroupIDAdmin,
                    @ForumID,
                    @AccessMaskIDAdmin)
        INSERT INTO yaf_ForumAccess
                   (GroupID,
                    ForumID,
                    AccessMaskID)
        VALUES     (@GroupIDGuest,
                    @ForumID,
                    @AccessMaskIDReadOnly)
        INSERT INTO yaf_ForumAccess
                   (GroupID,
                    ForumID,
                    AccessMaskID)
        VALUES     (@GroupIDMember,
                    @ForumID,
                    @AccessMaskIDMember)
    END


